#include <bits/stdc++.h>
using namespace std;
typedef pair<string, string> pss;
int main() {
    ios_base::sync_with_stdio(false);cin.tie(nullptr);
    vector<pss> s;// author - title
    map<string, int> ID;// title, ID
    string in;
    while(getline(cin, in) && in!="END"){
        string title, author;
        int pos = in.find('"', 1);
        title = in.substr(1, pos - 1);
        author = in.substr(pos+5);
        s.emplace_back(make_pair(author, title));
    }

    sort(s.begin(), s.end());
    for (int i = 0; i < s.size(); ++i) {
        ID[s[i].second] = i;
    }
    vector<bool> on(s.size(), true);
    vector<pss> todo; // author - title
    while(getline(cin, in) && in!="END"){
        string cmd = in.substr(0, 6);
        if(cmd == "SHELVE"){
            sort(todo.begin(), todo.end());
            for (const auto& i:todo){
                string t = i.second;
                int id = ID[t];
                bool f = false;
                for (int j = id-1; j >=0 ; --j) {
                    if(on[j]){
                        string af = s[j].second;
                        cout << "Put \""  << t << "\" after \"" << af << "\"\n";
                        f = true;
                        break;
                    }
                }
                if(!f) cout << "Put \"" << t << "\" first\n";
                on[id] = true;
            }
            cout << "END\n";
            todo.clear();
        }
        else{
            int pos = in.find('"', 8);
            string title = in.substr(8, pos - 8);
            int id = ID[title];
            if(cmd == "BORROW"){
                on[id] = false;
            }
            if(cmd == "RETURN"){
                string author = s[id].first;
                todo.emplace_back(make_pair(author, title));
            }
        }


    }
    return 0;
}